﻿using System;
using System.Net;
using System.Collections.Generic;
using SomeKit.Serialization;

#if WINDOWS_PHONE
using Microsoft.Phone.Reactive;
#else
using System.Linq;
#endif
using SomeKit.Rx.Http;
namespace SomeKit.Rx.TypeSafe
{
    public static partial class ClientHelper
    {
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="serializer">A method that is capable of serializing <paramref name="body"/>.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static IObservable<string> Put<BODY>(this Uri uri, BODY body, Func<BODY, string> serializer, Action<HttpWebRequest> headerHandler = null)
        {
            return uri.Put(serializer(body), headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static IObservable<string> PutXml<BODY>(this Uri uri, BODY body, Action<HttpWebRequest> headerHandler = null)
        {
            return uri.PutXml(body.SerializeXml(), headerHandler);
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static IObservable<string> PutJson<BODY>(this Uri uri, BODY body, Action<HttpWebRequest> headerHandler = null)
        {
            return uri.PutJson(body.SerializeJson(), headerHandler);
        }

        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="serializer">A method that is capable of serializing <paramref name="body"/>.</param>
        /// <param name="deserializer">A method capable of deserializing the resultant object.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static IObservable<RESULT> Put<BODY, RESULT>(this Uri uri, BODY body, Action<RESULT> handler, Action<Exception> errorHandler,
            Func<BODY, string> serializer, Func<string, RESULT> deserializer, Action<HttpWebRequest> headerHandler = null)
        {
            return uri.Put(serializer(body), headerHandler).Select(s => deserializer(s));
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in XML.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="handler">The handler receiving the resultant object.</param>
        /// <param name="errorHandler">The error handler.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static IObservable<RESULT> PutXml<BODY, RESULT>(this Uri uri, BODY body, Action<HttpWebRequest> headerHandler = null)
        {
            return uri.PutXml(body.SerializeXml(), headerHandler).Select(s => s.DeserializeXml<RESULT>());
        }
        /// <summary>
        /// Executes the "PUT" verb against the resource located at <paramref name="uri"/>. The request body is expected to be formatted in JSON.
        /// </summary>
        /// <typeparam name="BODY">A type of object to be sent to the server.</typeparam>
        /// <typeparam name="RESULT">The type of the result parameter.</typeparam>
        /// <param name="uri">The resource to be accessed.</param>
        /// <param name="body">The body of the request.</param>
        /// <param name="headerHandler">Custom header handler.</param>
        public static IObservable<RESULT> PutJson<BODY, RESULT>(this Uri uri, BODY body, Action<HttpWebRequest> headerHandler = null)
        {
            return uri.PutXml(body.SerializeJson(), headerHandler).Select(s => s.DeserializeJson<RESULT>());
        }
    }
}